home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / 1993-OCT.ZIP / BETA.ASC < prev    next >
Text File  |  1993-09-17  |  11KB  |  433 lines

  1. _THE BETA PROGRAMMING LANGUAGE_
  2. by Steve Mann
  3.  
  4. [LISTING ONE]
  5.  
  6. ORIGIN '~beta/macenv/v3.5/basicmacenv' (* part of the fragment system *)
  7. --- program: descriptor ---(* for handling code modules *)
  8.  
  9. MacEnv (* include the basic app framework *)
  10. (#
  11.    myWindow: @window (#
  12.       type::< windowTypes.zoom;
  13.       hasClose::< trueObject;
  14.       open::< (#
  15.          do  (100, 100)  -> position;
  16.              (300, 100)   -> size;
  17.              My window'  -> title;
  18.    #); #);
  19.  FileMenu::< (#
  20.    iNew::< (# hit::< (# do myWindow.open #); #);
  21.    iOpen::<    (# hit::< (# do myWindow.open #); #);
  22. #); #)
  23.  
  24.  
  25. [LISTING TWO]
  26.  
  27. ORIGIN '~beta/macenv/v3.5/basicmacenv'
  28. [[
  29. --- INCLUDE '~beta/macenv/v3.5/fields'
  30. --- INCLUDE '~beta/macenv/v3.5/macfile'
  31. --- INCLUDE '~beta/basiclib/v1.3/file'
  32. --- program:descriptor---
  33.  
  34. MacEnv (#
  35. (* === Font Menu Patterns === *)
  36.    FontMenu: @Menu (#
  37.       fontSizes: [4] @integer;
  38.       firstFontOset: @integer;
  39.       theEditor: ^edWindow;
  40.       Name::< (# do 'Font' -> theName #);
  41. (*    create the font menu--names on top, sizes on bottom. the
  42. *     handler distinguishes between the two using the item number. *)
  43.       open::< (#
  44.       do  System.AvailableFonts
  45.           (# do thisFontName[] -> Append #);
  46.           separator -> Append;
  47.           '9' -> Append -> firstFontOSet; '12' -> Append;
  48.           '14' -> Append; '18' -> Append;
  49.           9  -> fontSizes [1];12 -> fontSizes [2];
  50.           14 -> fontSizes [3];18 -> fontSizes [4];
  51.       #);
  52. (*    override the standard event handler for this menu
  53. *     to change the font or size of the current selection *)
  54.       EventHandler::< (#
  55.          EvalStatus::<   (# Done::< trueObject #);
  56.          Select::< (#
  57.          do  (if theEditor[] <> None // true then
  58.                  (if theItem.ItemNumber >= firstFontOSet // true then
  59.                     fontSizes [theItem.ItemNumber - firstFontOset + 1] ->
  60.                       theEditor.ChangeSize;
  61.                  else theItem.name -> theEditor.ChangeFont
  62.          if);if);
  63.    #); #); #);
  64. (* === Editor Window Patterns === *)
  65.    edWindow: Window (#
  66.       Type::< WindowTypes.Zoom;   HasClose::< TrueObject;
  67.       myText: ^StyledText;myFile: ^macfile;
  68.       opened: @boolean;
  69. (*    change the font type of the current text selection *)
  70.       ChangeFont: (#
  71.           aTextStyle: @TextStyle; start,end: @Integer
  72.           enter aTextStyle.name
  73.           do  myEditor.Contents.Selection -> (start, end);
  74.               (start, end, aTextStyle.FontID) ->
  75.                       myEditor.Contents.SetOneFont
  76.       #);
  77. (*    change the font size of the current text selection *)
  78.       ChangeSize: (#
  79.           fontSize, start,end: @Integer
  80.           enter fontSize
  81.           do  myEditor.Contents.Selection -> (start, end);
  82.               (start, end, fontSize) ->
  83.                       myEditor.Contents.SetOneSize
  84.       #);
  85. (*    create a dynamic menu item for the File/Close command *)
  86.       DoClose: @theFileMenu.action (#
  87.           hit::< (# do close #); #);
  88. (*    create a dynamic menu item for File/Save command *)
  89.       DoSave: @theFileMenu.action (#
  90.           hit::< (#
  91.       do  (if opened // true then myEditor.saveMyEd
  92.                  else myEditor.saveAsMyEd if)
  93.       #); #);
  94. (*    create a dynamic menu item for File/Save As command *)
  95.       DoSaveAs: @theFileMenu.action (#
  96.           hit::< (# do myEditor.saveAsMyEd #); #);
  97. (*    Window event handler--enable/disable the font menu and dynamic 
  98. *     File commands as current window gets activated / deactivated *)
  99.       EventHandler::< (#
  100.       Activate::< (#
  101.           do  this (edWindow)[] -> FontMenu.theEditor[];
  102.                FontMenu.enable;
  103.                DoClose[]   -> theFileMenu.CloseItem.Attach;
  104.                DoSave[]    -> theFileMenu.SaveItem.Attach;
  105.                DoSaveAs[]  -> theFileMenu.SaveAsItem.Attach;
  106.       #);
  107.       Deactivate::< (#
  108.            do  none -> FontMenu.theEditor[];   ;
  109.                 FontMenu.disable;
  110.                 theFileMenu.CloseItem.Detach;
  111.                 theFileMenu.SaveItem.Detach;
  112.                 theFileMenu.SaveAsItem.Detach;
  113.       #);#);
  114. (*    pattern to open an existing file *)
  115.       openFile: (#
  116.            do  newFile;
  117.               (if ('','Select a File to Edit: ') ->
  118.                    myFile.GetFile -> opened // true then
  119.                    myFile.RestoreStyledText -> myText[]
  120.                if)
  121.        exit opened
  122.        #);
  123. (*     pattern to create a new file *)
  124.        newFile:(#
  125.            do  &StyledText[] -> myText[];
  126.                &macfile[] -> myFile[]
  127.        #);
  128. (*     pattern to open a new window *)
  129.        open::< (#
  130.           do  'Untitled' -> title;
  131.               (if opened // true then
  132.                    myFile.entry.path.name -> title
  133.                if);
  134.                nextPosition -> Position;   (350,400) -> Size;
  135.                (5,10) -> nextPosition.add; myEditor.open
  136.        #);
  137. (* === Text Editor Patterns--scoped inside Window to have ===
  138. *  === access to all of the Window attributes === *)
  139.    myEditor: @TextEditor (#
  140. (*     track text changes (to disable Save if no changes). *)
  141.        docChanged: @boolean;
  142.        ContentsDesc::< (#
  143.            Textchanged::< (# do true -> docChanged #);
  144.        #);
  145. (*  open (create) a new text editor--bind to window *)
  146.     open::< (#
  147.        do  this(edWindow).Size->Size;
  148.        true->BindBottom->BindRight;
  149.        myText[] -> Contents.contents;
  150.        myEditor[] -> Target;
  151.        false -> docChanged
  152.    #);
  153. (*  save the current document to disk *)
  154.     saveMyEd: (#
  155.        do  contents.contents -> myFile.SaveStyledText
  156.     #);
  157. (*  save the current doc to disk, but allow a name change *)
  158.     saveAsMyEd: (#
  159.        do  (if ('Select a Destination File Name: ',title) ->
  160.                myFile.PutFile // true then saveMyEd;
  161.                myFile.entry.path.name -> title
  162.             if)
  163.      #);
  164. (*   close and (optionally) save the current text editor *)
  165.      close::< (#
  166.          do  (if docChanged // true then saveAsMyEd if)
  167.      #);#); #);
  168. (* === File Menu Patterns === *)
  169.    FileMenu::< (#
  170. (* File/Open command *)
  171.    iOpen::< (# hit::< (#
  172.       myWindow:^edWindow;
  173.       do  &edWindow[] -> myWindow[];
  174.           (if myWindow.openFile // true then
  175.               myWindow.open
  176.           if)
  177.     #);#);
  178. (* File/New command *)
  179.    iNew::< (# hit::< (#
  180.        myWindow:^edWindow
  181.        do  &edWindow[] -> myWindow[];
  182.             myWindow.newFile;
  183.             myWindow.open
  184.    #);#);#);
  185. (* === Program start - create the font menu, put it on the ===
  186. *  === menu bar, and set the first window tiling position. === *)
  187.    nextPosition: @Point;
  188.    do  FontMenu.Open;
  189.           FontMenu.disable;
  190.           FontMenu[] -> Menubar.Append;
  191.           (5,40) -> nextPosition
  192. #)
  193. ---]]
  194.  
  195.  
  196.  
  197.  
  198. Example 1: 
  199.  
  200. <Pattern Name>: (#
  201.   <Attribute 1> ;
  202.   <Attribute 2> ;
  203.  .
  204.   <Attribute N> ;
  205.  
  206.   Enter <Input list>
  207.   Do <Imperatives>
  208.   Exit <Output list>
  209. #);
  210.  
  211.  
  212. Example 2: 
  213.  
  214. Account: (# (* a simple bank account class pattern *)
  215.   balance: @integer;  (* bank account balance *)
  216.   Deposit: (# (* add `amount' to balance *)
  217.     amount: @integer(* local declaration *)
  218.     enter amount(* input list *)
  219.     do balance + amount -> balance  (* action *)
  220.     exit balance(* output list *)
  221. #);
  222. Withdraw: (#(* subtract `amount' from balance *)
  223.     amount: @integer
  224.     enter amount
  225.     do balance - amount -> balance
  226.     exit amount
  227. #);  #);
  228.  
  229. Example 3: 
  230.  
  231. (#  (* a program pattern with no name *)
  232.   Account: (# (*  a pattern declaration within the unnamed pattern *)
  233.     acct_balance: @ integer;
  234.     Deposit: (#
  235.        amount: @ integer
  236.        enter amount
  237.        do acct_balance + amount -> acct_balance
  238.        exit balance
  239. #);
  240. Withdraw: (#
  241.     amount: @ integer
  242.     enter amount
  243.     do acct_balance - amount -> acct_balance
  244.     exit amount
  245. #);
  246. Balance: (#
  247.    exit acct_balance
  248. #); #);
  249.  
  250. A: @ Account;
  251. cash_on_hand, balance: @ integer;
  252.  
  253. do  100 -> &A.Deposit;
  254.     250 -> &A.Deposit;
  255.     75  -> &A.Withdraw -> cash_on_hand; (* $75 on hand *)
  256.     &A.Balance -> balance;  (* $275 balance *)
  257. #)
  258.  
  259.  
  260. Example 4: 
  261.  
  262. (a)
  263.  
  264. <Name>: [size] @<Type> ;(* static repetition *)
  265. <Name>: [size] ^<Type> ;(* dynamic repetition *)
  266.  
  267. (b)
  268.  
  269. (for <Index>: <Range> repeat <Imperative-list> for)
  270.  
  271.  
  272. (c)
  273.  
  274. (if E0
  275.    // E1 then I1
  276.    // E2 then I2
  277.    . . .
  278.    //En then In
  279.    else I
  280. if)
  281.  
  282.  
  283. Example 5: 
  284.  
  285. (#
  286. Power: (#   (* compute X^n where n > 0 *)
  287. X, Y: @ real;   n: @ integer;
  288. enter (X, n)
  289. do  1 -> Y;
  290. (for i: n repeat Y * X -> Y for)
  291. exit Y
  292. #)
  293.  
  294. Reciproc: (#    (* compute (Q, 1/Q) *)
  295. Q, R: @ real;
  296. enter Q
  297. do  (if (Q // 0) then 0 -> R
  298. else (1 div Q) -> R
  299. if )
  300. exit (Q, R)
  301. #);
  302.  
  303. A, B: @ real;
  304. do  (3.14, 2) -> &Power -> &Reciproc -> (A, B);
  305. (* A = 3.14 ^ 2, B = 1/A *)
  306. #)
  307.  
  308.  
  309. Example 6: 
  310.  
  311. Reservation:(#
  312. Date:   @DateType;
  313. Customer:   ^CustomerRecord
  314. #)
  315.  
  316. FlightReservation: Reservation  (#
  317. FlightNo:   ^Flight;
  318. SeatNo: ^Seat
  319. #)
  320.  
  321. TrainReservation: Reservation   (#
  322. TrainNo:^Train;
  323. CarriageNo: ^Carriage;
  324. SeatNo: ^Seat
  325. #)
  326.  
  327.  
  328. Example 7: 
  329.  
  330. ReservationRegister: (#
  331. Table: [100] ^ Reservation; (* arbitrary size restriction *)
  332. top: @integer;
  333.  
  334. Insert: (#  (* insert a reservation *)
  335. R: ^ Reservation;
  336. enter R []
  337. do R [] -> Table [top+1 -> top] []
  338. #);
  339.  
  340. NoOfRes: (# (* get number of reservations in system *)
  341. exit top
  342. #)
  343.  
  344. GetRes: (#  (* get reservation number inx *)
  345. inx: @ integer
  346.  
  347. enter inx
  348. exit Table [inx] [];
  349. #); #)
  350.  
  351. Example 8: 
  352.  
  353. (a)
  354.  
  355. P1: (# a: @integer  do 1 -> a   #);
  356. P2: P1  (# b: @integer  do 2 -> b   #);
  357. P3: P2  (# c: @integer  do 3 -> c   #);
  358.  
  359.  
  360. (b)
  361.  
  362. P3 invoked  P2 invoked  P1 invoked  1 -> a  2 -> b  3 -> c
  363.  
  364.  
  365. (c)
  366.  
  367. P1: (# a: @ integer do INNER P1;1 -> a  #);
  368. P2: P1  (# b: @ integer do 2 -> b;  INNER P2#);
  369. P3: P2  (# c: @ integer do 3 -> c   #);
  370.  
  371.  
  372. (d)
  373.  
  374. P3  P2  P1  P2 (via INNER P1)   2 -> b
  375. P3 (via INNER P2)   3 -> c  1 -> a
  376.  
  377.  
  378. Example 9: Building a virtual pattern
  379.  
  380. Reservation: (#
  381.   Date: @ DateType;
  382.   Customer: ^ CustomerRecord
  383.   Display: (#
  384.     do  Date.Display;
  385.        Customer.Display; INNER
  386. #); #)
  387.  
  388. FlightReservation: Reservation (#
  389.   FlightNo: ^ Flight;
  390.   SeatNo: ^ Seat
  391.   Display: (#
  392.     do  FlightNo.Display;
  393.        Customer.Display
  394. #); #)
  395.  
  396. TrainReservation: Reservation (#
  397.   TrainNo: ^Train;
  398.   CarriageNo: ^Carriage;
  399.   SeatNo: ^ Seat
  400.   Display: (#
  401.      do  TrainNo.Display;
  402.         CarriageNo.Display;
  403.         SeatNo.Display
  404. #); #)
  405.  
  406. Example 10: 
  407.  
  408. Reservation: (#
  409.    Date: @DateType; Customer  ^CustomerRecord
  410.    Display:< (#
  411.       do  Date.Display;
  412.         Customer.Display; inner
  413. #); #)
  414.  
  415. FlightReservation: Reservation (#
  416.    FlightNo: ^Flight;
  417.    SeatNo: ^Seat
  418.    Display::< (#
  419.       do  FlightNo.Display;
  420.           Customer.Display
  421. #); #)
  422.  
  423. TrainReservation: Reservation (#
  424.    TrainNo: ^Train;
  425.    CarriageNo: ^Carriage;
  426.    SeatNo: ^Seat
  427.    Display::< (#
  428.      do  TrainNo.Display;
  429.        CarriageNo.Display;
  430.        SeatNo.Display
  431. #); #)
  432.  
  433.